#include "FullScreenQuad.h"
#include <GL/glew.h>
#include "common.h"
#include "ShaderManager.h"

namespace Tmpl {

static GLuint mVBO = 0;
static GLuint mVAB = 0;

void Init(ShaderProgram* shader)
{
	float quad[] =
	{
		-1.0f,  1.0f,	// v0 - top left corner
		-1.0f, -1.0f,	// v1 - bottom left corner
		 1.0f,  1.0f,	// v2 - top right corner
		 1.0f, -1.0f	// v3 - bottom right corner
	};

	glGenVertexArrays(1, &mVAB);
	glBindVertexArray(mVAB);

	glGenBuffers(1, &mVBO);
	glBindBuffer(GL_ARRAY_BUFFER, mVBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW);

	GLint vertexLoc = shader->GetAttribLoc("InVertex");
	glVertexAttribPointer(vertexLoc, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
	glEnableVertexAttribArray(vertexLoc);

	glBindVertexArray(0);
}
void FullScreenQuad::Destroy()
{
	if (mVAB)
	{
		glDeleteVertexArrays(1, &mVAB);
		mVAB = 0;
	}
	if (mVBO)
	{
		glDeleteBuffers(1, &mVBO);
		mVBO = 0;
	}
}
void FullScreenQuad::Draw(ShaderProgram* shader)
{
	if (!mVAB) Init(shader);

	glBindVertexArray(mVAB);
	
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 8);

	glBindVertexArray(0);
}

}